{% extends "indoors.html" %}
{% load humanize %}

{% block head %}
<script>
jQuery(document).ajaxSend(function(event, xhr, settings) {
    function getCookie(name) {
        var cookieValue = null;
        if (document.cookie && document.cookie != '') {
            var cookies = document.cookie.split(';');
            for (var i = 0; i < cookies.length; i++) {
                var cookie = jQuery.trim(cookies[i]);
                // Does this cookie string begin with the name we want?
                if (cookie.substring(0, name.length + 1) == (name + '=')) {
                    cookieValue = decodeURIComponent(cookie.substring(name.length + 1));
                    break;
                }
            }
        }
        return cookieValue;
    }
    function sameOrigin(url) {
        // url could be relative or scheme relative or absolute
        var host = document.location.host; // host + port
        var protocol = document.location.protocol;
        var sr_origin = '//' + host;
        var origin = protocol + sr_origin;
        // Allow absolute or scheme relative URLs to same origin
        return (url == origin || url.slice(0, origin.length + 1) == origin + '/') ||
            (url == sr_origin || url.slice(0, sr_origin.length + 1) == sr_origin + '/') ||
            // or any other URL that isn't scheme relative or absolute i.e relative.
            !(/^(\/\/|http:|https:).*/.test(url));
    }
    function safeMethod(method) {
        return (/^(GET|HEAD|OPTIONS|TRACE)$/.test(method));
    }

    if (!safeMethod(settings.type) && sameOrigin(settings.url)) {
        xhr.setRequestHeader("X-CSRFToken", getCookie('csrftoken'));
    }
});

function voteup() {
  review_id = {{review.pk}};
  $(".votes").fadeTo(300, 0);
  $.post('/a/vote/' + review_id, {
      command: 'up'
    }, function(data){
      if (data.success == 'true') {
        $("#message").html(data.message);
      } else {
        $("#message").html(data.message);
      }
      $(".votes").text(data.votes);
      $(".votes").buttonMarkup();
      $(".voting").controlgroup();
      $(".votes").fadeTo(300, 1);
    }, 'json')
}

function votedown() {
  review_id = {{review.pk}};
  $(".votes").fadeTo(300, 0);
  $.post('/a/vote/' + review_id, {
      command: 'down'
    }, function(data){
      if (data.success == 'true') {
        $("#message").html(data.message);
      } else {
        $("#message").html(data.message);
      }
      $(".votes").text(data.votes);
      $(".votes").buttonMarkup();
      $(".voting").controlgroup();
      $(".votes").fadeTo(300, 1);
    }, 'json')
}


$(document).ready(function(){    
  $(".voteup").click(voteup);
  $(".votedown").click(votedown);
  $(".delete").click(function() {
    $("#delete_form").submit();
  });
});
</script>
{% endblock head %}

{% block title %}
{{phone_number}}
{% if is_self %}
  (Me)
{% endif %}
{% endblock title %}

{% block content %}
<form id="delete_form" action="/a/review/{{review.pk}}" method="POST">
  {% csrf_token %}
  <input type="hidden" name="command" value="delete">
</form>
<h2>
  Review of {{review.number}} by 
  {% if review.user.get_profile.number == phone_number %}
    Owner
  {% elif review.user == user %}
    {{review.poster_number_display}} - Me
  {% else %}
    {{review.poster_number_display}}
  {% endif %}
</h2>
<h3>Posted {{review.date|naturaltime}}</h3>
<span data-role="controlgroup" data-type="horizontal" class="voting">
  <span data-role="button" class="votes">{{review.votes}}</span>
  <span data-role="button" data-inline="true" data-icon="arrow-u" class="voteup">Vote Up</span>
  <span data-role="button" data-inline="true" data-icon="arrow-d" class="votedown">Vote Down</span>
</span>
{% if is_reviewer %}
<span data-role="controlgroup" data-type="horizontal" class="actions">
  <span data-role="button" data-inline="true" data-theme="e" class="delete">
    Delete
  </span>
</span>
{% endif %}
<ul data-role="listview" data-inset="true">
    <li class="review_item {% if review.user == user %}review_owner{% endif %}" id="review-{{review.pk}}">
      {{review.text}}
    </li>
</ul>
<div data-role="controlgroup" data-type="horizontal" class="voting">
  <span data-role="button" class="votes">{{review.votes}}</span>
  <span data-role="button" data-inline="true" data-icon="arrow-u" class="voteup">Vote Up</span>
  <span data-role="button" data-inline="true" data-icon="arrow-d" class="votedown">Vote Down</span>
</div>
{% endblock %}